﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using chad.azure.diagnostics.data;

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;

namespace chad.azure.diagnostics.wad
{
    public class PerformanceCounterDataSourceProcessor 
    {
        private String _StorageAccountName = "";
        private String _StorageAccountKey = "";
        private String _DeploymentId = "";
        private String _RoleName = "";

        public PerformanceCounterDataSourceProcessor(String storageAccountName, String storageAccountKey, String deploymentId = null, String roleName = null)
        {
            _StorageAccountName = storageAccountName;
            _StorageAccountKey = storageAccountKey;
            _DeploymentId = deploymentId;
            _RoleName = roleName;   
        }

        public List<Metric> GetMetrics(DateTime startDateTime, DateTime endDateTime) //, object[] args)
        {
            //http://blogs.msdn.com/b/windowsazurestorage/archive/2012/11/06/windows-azure-storage-client-library-2-0-tables-deep-dive.aspx
            const String tableName = "WADPerformanceCountersTable";

            List<Metric> metrics = new List<Metric>();
            
            //TODO:
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(StorageAccountHelper.GetConnectionString(_StorageAccountName,_StorageAccountKey));
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            
            tableClient.DefaultRequestOptions.PayloadFormat = TablePayloadFormat.JsonNoMetadata;

            CloudTable table = tableClient.GetTableReference(tableName);

            String startingPartition = WadHelper.GenerateWadPartitionKeyFromUtcDateTime(startDateTime);
            String endingPartition = WadHelper.GenerateWadPartitionKeyFromUtcDateTime(endDateTime);

            IQueryable<PerformanceCounterEntity> query = 
                (from p in table.CreateQuery<PerformanceCounterEntity>()
                where string.Compare(p.PartitionKey, startingPartition) >= 0 &&
                string.Compare(p.PartitionKey, endingPartition) <= 0
                select p);

            if(_DeploymentId  != null && _DeploymentId != String.Empty)
            {
                query = query.Where(q => q.DeploymentId == _DeploymentId);
            }

            if(_RoleName != null && _RoleName != String.Empty)
            {
                query = query.Where(q => q.Role == _RoleName);
            }

            foreach (var p in query.AsEnumerable())
            {
                Metric m = new Metric();

                m.SampledDateTime = new DateTime(p.EventTickCount);
                m.ObjectIdentifier = p.RoleInstance;
                m.FullyQualifiedMetricName = p.CounterName;
                m.DateTimeTimeZone = "UTC";
                m.Value = double.Parse(p.CounterValue);

                metrics.Add(m);
            }

            return metrics;
        }
    }
}
